import { defineStore } from "pinia";
import { store } from "/@/store";
import { userType } from "./types";
import { router } from "/@/router";
import { routerArrays } from "/@/layout/types";
import { storageSession } from "@pureadmin/utils";
import { getLogin } from "/@/api/system/user";
import { getAccessToken, getRefreshToken, setAccessToken, setRefreshToken, clearAccessToken, isTokenExp } from "/@/utils/auth";
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
import wsCache from "/@/utils/storage";

export const useUserStore = defineStore({
  id: "pure-user",
  state: (): userType => ({
    token: getAccessToken(),
    refreshToken: getRefreshToken(),
    name: 'yell'
  }),
  actions: {
    setToken(token: string) {
      this.token = token;
      setAccessToken(token)
    },
    setRefreshToken(refreshToken: string) {
      this.refreshToken = refreshToken
      setRefreshToken(refreshToken)
    },
    isTokenExp() {
      return isTokenExp();
    },
    /** 登出 清空缓存 */
    logOut() {
      this.token = '';
      this.refreshToken = ''
      this.name = '';
      clearAccessToken()

      dispatchEvent(new CustomEvent('logout'))
    }
  }
});

export function useUserStoreHook() {
  return useUserStore(store);
}
